package com.qin.controller;

import com.alibaba.fastjson.JSONObject;
import com.qin.entity.TInterface;
import com.qin.enums.RetCode;
import com.qin.service.InterfaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;

@Controller("interfaceController")
@RequestMapping("/interface")
@ResponseBody
public class InterfaceController {

    @Autowired
    private InterfaceService interfaceService;

    @RequestMapping(value="/queryInterfaces", consumes="application/json;charset=UTF-8", produces="application/json;charset=UTF-8")
    public String queryInterfaces(@RequestBody String body){
//        System.out.println("web层：queryInterfaces");
        System.out.println("接口调用：/interface/queryInterfaces");

        //入参封装到实体类
        TInterface tInterface = JSONObject.parseObject(body, TInterface.class);
        System.out.println("接口入参：" + body);
        //调用service层
        String retString = interfaceService.queryInterfaces(tInterface).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/insertInterface", produces="text/html;charset=UTF-8")
    public String insertInterface(@RequestBody String body){
//        System.out.println("web层：insertInterface");
        System.out.println("接口调用：/interface/insertInterface");

        //入参封装到实体类
        TInterface tInterface = JSONObject.parseObject(body, TInterface.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((tInterface.getSystem_name() == null) || ("".equals(tInterface.getSystem_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "system_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((tInterface.getInterface_name() == null) || ("".equals(tInterface.getInterface_name()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "interface_name不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }
        else if((tInterface.getInterface_url() == null) || ("".equals(tInterface.getInterface_url()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "interface_url不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = interfaceService.insertInterface(tInterface).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/deleteInterface", produces="text/html;charset=UTF-8")
    public String deleteInterface(@RequestBody String body){
//        System.out.println("web层：deleteInterface");
        System.out.println("接口调用：/interface/deleteInterface");

        //入参封装到实体类
        TInterface tInterface = JSONObject.parseObject(body, TInterface.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((tInterface.getId() == null) || ("".equals(tInterface.getId()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = interfaceService.deleteInterface(tInterface).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }

    @RequestMapping(value="/updateInterface", produces="text/html;charset=UTF-8")
    public String updateInterface(@RequestBody String body){
//        System.out.println("web层：updateInterface");
        System.out.println("接口调用：/interface/updateInterface");

        //入参封装到实体类
        TInterface tInterface = JSONObject.parseObject(body, TInterface.class);
        System.out.println("接口入参：" + body);
        //入参非空判断
        if((tInterface.getId() == null) || ("".equals(tInterface.getId()))){
            Map errMap = new HashMap();
            errMap.put("retCode", RetCode.ERROR.getCode());
            errMap.put("retMsg", RetCode.ERROR.getMsg());
            errMap.put("errorMsg", "id不能为空");
            String retString = new JSONObject(errMap).toString();

            System.out.println("接口出参：" + retString);
            return retString;
        }

        //调用service层
        String retString = interfaceService.updateInterface(tInterface).toString();

        System.out.println("接口出参：" + retString);
        return retString;
    }
}
